package com.ruoyi.lowcode.service.impl;

import com.ruoyi.lowcode.database.DatabaseMain;
import com.ruoyi.lowcode.domain.LcDatabaseSource;
import com.ruoyi.lowcode.domain.bo.LcDatabaseSourceBo;
import com.ruoyi.lowcode.domain.bo.SQLBo;
import com.ruoyi.lowcode.domain.vo.LcDatabaseSourceVo;
import com.ruoyi.lowcode.mapper.LcDatabaseSourceMapper;
import com.ruoyi.lowcode.service.ILcDatabaseSourceService;
import com.ruoyi.lowcode.service.ISQLService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Service
public class SQLServiceImpl implements ISQLService {

    private final LcDatabaseSourceMapper databaseSourceMapper;

    @Override
    public List<Map<String, Object>> selectListSql(SQLBo sqlBo) throws ClassNotFoundException {
        LcDatabaseSourceVo databaseSource = databaseSourceMapper.selectVoById(sqlBo.getDatabaseSourceId());
        return DatabaseMain.getSource(databaseSource.getType()).selectList(databaseSource, sqlBo.getSelectListSql());
    }

    @Override
    public boolean testConnection(LcDatabaseSourceBo bo) throws ClassNotFoundException {
        DatabaseMain.getSource(bo.getType()).connection(bo);
        return true;
    }

}
